package chartCraft.graphDesk;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import chartCraft.ChartCraftController;
import chartCraft.model.LocationModel;
import chartCraft.model.Model;
import chartCraft.model.Model.NotifyingMessage;
import chartCraft.model.TopModel;

public class DraggableChart extends DraggableItem {

	private static final long serialVersionUID = 1L;
	private LocationModel mModel;
	private final ChartCraftController mChartCraftController;
	private final ChartLabel mChartLabel;
	
	public DraggableChart(ChartCraftController chartCraftController, LocationModel locationModel, TopModel topModel) {
		//super(new ChartViewPanel(model.getChartModel()));
		super();
		mChartLabel = new ChartLabel(locationModel, topModel);
		
		add(mChartLabel);
		
		mChartCraftController = chartCraftController;
		setModel(locationModel);
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		if (arg0 == mModel) {
			
			NotifyingMessage msg = (NotifyingMessage) arg1;
			String action = msg.getAction();
			
			if (action == LocationModel.SELECTION_STATUS) {
				boolean isSelect = (Boolean) msg.getObject();
				showResizableBorder(isSelect);
			} else if(action == LocationModel.MOVE) {
				Point newLocation = (Point) msg.getObject();
				moveTo(newLocation);
			} else if(action == LocationModel.RESIZE) {
				Dimension newSize = (Dimension) msg.getObject();
				resizeTo(newSize);
			} 
		}
	}

	@Override
	public void setModel(Model model) {
		mModel = (LocationModel) model;
		model.addGraphViewObserver(this);
		
		repaint();
	}

	@Override
	public LocationModel getModel() {
		return mModel;
	}

	@Override
	protected void beingSelected() {
		int locationId = mChartCraftController.getTopModel().getLocationId(mModel);
		mChartCraftController.setSelectionOnly(locationId, locationId+1);
	}

	@Override
	public void draggingEnd(Point startPoint, Point endPoint, Dimension startSize, Dimension endSize) {
		mChartCraftController.moveAndResizeLocationModel(mModel, startPoint, endPoint, startSize, endSize);
	}

	@Override
	public JPopupMenu getPopUpMenu() {
		JPopupMenu result = new JPopupMenu();
		JMenuItem deleteItem = new JMenuItem("Delete");
		result.add(deleteItem);
		deleteItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				mChartCraftController.deleteLocation(mModel);				
			}
		});
		if (mChartCraftController.getTopModel().getNoOfLocations() <= 1) {
			deleteItem.setEnabled(false);
		}
		
		
		result.addSeparator();
		
		JMenuItem bringToFrontItem = new JMenuItem("Bring to Front");
		result.add(bringToFrontItem);
		bringToFrontItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				mChartCraftController.bringToFront(mModel);				
			}
		});
		
		JMenuItem bringToBackItem = new JMenuItem("Bring to Back");
		result.add(bringToBackItem);
		bringToBackItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				mChartCraftController.bringToBack(mModel);				
			}
		});
		return result;
	}
}
